package com.cke.yang.storage.app.service.impl;

import com.cke.yang.common.infra.enums.SaveActionEnum;
import com.cke.yang.common.infra.enums.StorageSourceEnum;
import com.cke.yang.common.infra.utils.UserDetailsUtils;
import com.cke.yang.storage.api.dto.request.StgSourceListRequest;
import com.cke.yang.storage.app.service.StgSourceService;
import com.cke.yang.storage.domain.entity.StgSource;
import com.cke.yang.storage.domain.repository.StgSourceRepository;
import com.cke.yang.storage.infra.mapper.StgSourceMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author: luozhaojian
 * @description:
 * @date: 2025/10/7 10:56
 */
@Service
public class StgSourceServiceImpl implements StgSourceService {

    @Autowired
    private StgSourceRepository stgSourceRepository;

    @Autowired
    private StgSourceMapper stgSourceMapper;

    @Override
    public StgSource save(StgSource stgSource) {
        // 校验action
        String action = stgSource.get$action();
        if (!SaveActionEnum.support(action)) {
            throw new RuntimeException("执行动作未匹配，请联系管理员解决");
        }
        SaveActionEnum actionEnum = SaveActionEnum.getByCode(action);
        // 数据基本校验
        this.checkData(stgSource, actionEnum);
        // 填充字段
        this.fillData(stgSource);
        // 执行动作
        return stgSourceRepository.saveByAction(actionEnum, stgSource);
    }

    @Override
    public List<StgSource> findByUserId(Long userId) {
        return stgSourceRepository.findBySourceOwner(userId);
    }

    @Override
    public List<StgSource> list(StgSourceListRequest request) {
        QueryWrapper<StgSource> queryWrapper = new QueryWrapper<>();

        if (StringUtils.hasText(request.getSourceName())) {
            queryWrapper.lambda().like(StgSource::getSourceName, request.getSourceName());
        }

        if (StringUtils.hasText(request.getSourceType())) {
            queryWrapper.lambda().eq(StgSource::getSourceType, request.getSourceType());
        }

        if (StringUtils.hasText(request.getSourceCode())) {
            queryWrapper.lambda().eq(StgSource::getSourceCode, request.getSourceCode());
        }

        if (request.getIsEnabled() != null) {
            queryWrapper.lambda().eq(StgSource::getIsEnabled, request.getIsEnabled());
        }

        queryWrapper.lambda().orderByDesc(StgSource::getSourceId);

        return stgSourceMapper.selectList(queryWrapper);
    }

    private void checkData(StgSource stgSource, SaveActionEnum actionEnum) {
        if (StorageSourceEnum.getByCode(stgSource.getSourceCode()) == null
                && !actionEnum.equals(SaveActionEnum.DELETE)) {
            throw new RuntimeException("数据源类型未匹配");
        }
    }


    private void fillData(StgSource stgSource) {
        StorageSourceEnum storageSourceEnum = StorageSourceEnum.getByCode(stgSource.getSourceCode());
        if (storageSourceEnum != null) {
            stgSource.setSourceType(storageSourceEnum.getType());
        }
        stgSource.setSourceOwner(UserDetailsUtils.getCurrentUserId());
    }

}
